<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * @author manh nguyen
 * @copyright 2013
 */
class register extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model();
    }

    //function _remap($method, $params = array()) {
//        $this->_view();
//    }


function  _remap($method, $params = array())
    {
        if(method_exists($this, $method))
        {
            return call_user_func_array(array($this, $method), $params);
        }
        $this->_view();
    }
	
    function _view() {
        $this->pub->set_tpl('register');//print_r(1); exit;
        $data = "";
        $content = $this->load->view('home', $data, true);       
        $this->pub->write('content', $content, true);       
        $this->pub->render();
    }
	
	function registers(){	
        $sex = $this->input->post('sex');     
        $post = $this->input->post('post');
        $post = json_decode($post, true);
		
		$data['username'] = $post['username'];
		$data['fullname'] =  $post['fullname'];
			$pass = $post['pass'].$data['username'];//cong user name va pass vao nhau
				$data['born'] = $post['born'];					
						$data['phone'] = $post['phone'];
							$data['email'] = $post['email'];							
							//su ly pass work
							$data['password'] = md5(md5(md5($pass)));
							
							//print_r($data);		
		//kiem tra xem username da ton tai chua
		$checkusername = $this->model->table("users")
									->select("id")
									->where("username", $data['username'])
									->find();
		if(isset($checkusername->id)){
			echo -1;
		}
		else{
			$data['fullname'] =  $post['fullname'];
			$pass = $post['pass'].$data['username'];//cong user name va pass vao nhau
				$data['born'] = $post['born'];					
						$data['phone'] = $post['phone'];
							$data['email'] = $post['email'];
							
							//su ly pass work
							$data['password'] = md5(md5(md5($pass)));
							
							//print_r($data);							
							$this->model->table("users")->save("", $data);
                                                        $cheklogin = new stdClass();
                                                        $cheklogin->username = $data["username"];  
                                                        $this->pub->SetSession("login", $cheklogin);
							
			echo 2;
		}
		
	}

}